﻿package com.eve.eplus.common.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.eve.eplus.common.tools.LogTools;

/**
 * 数据库操作类
 * @author Norsie
 * @create 12-11-28
 *
 */
public final class SQLiteAccess {

    private DBOpenHelper helper = null;

    private static SQLiteAccess mInstance = null;

    /**
     * 
     * @param context
     */
    private SQLiteAccess(Context context) {

        if (context == null) {
            LogTools.error("数据库错误：上下文(context)为空", null);
            return;
        }

        helper = new DBOpenHelper(context);

    }

    /**
     * 访问数据库实例
     * 
     * @param context
     * @return
     */
    synchronized public static SQLiteAccess getInstance(Context context) {

        if (mInstance == null) {
            LogTools.info("数据库消息:创建了一个新的SQLiteAccess");
            mInstance = new SQLiteAccess(context);
        }
        return mInstance;
    }

    /**
     * 执行RawQuery查询操作
     * @param sql
     * @param selectionArgs
     * @return
     */
    public Cursor execRawQuery (String sql, String[] selectionArgs) {
        
        SQLiteDatabase mDataBase = helper.getWritableDatabase();
        Cursor cursor = mDataBase.rawQuery(sql, selectionArgs);
        LogTools.info("数据库信息:sql:" + sql);
        return cursor;
    }

    /**
     * 
     * @param sql
     */
    public void execSQL(String sql) {

        SQLiteDatabase mDataBase = helper.getWritableDatabase();
        mDataBase.execSQL(sql);
        LogTools.info("数据库信息:sql:" + sql);
        closeDataBase(mDataBase);
    }

    /**
     * 插入数据
     * 
     * @param tableName
     * @param values
     */
    public void insert(String tableName, ContentValues values) {

        SQLiteDatabase db = helper.getWritableDatabase();
        db.insert(tableName, null, values);
        closeDataBase(db);
    }

    /**
     * 按系统的ID删除记录
     * 
     * @param tableName
     *            表名称
     * @param _id
     *            系统自带的ID
     */
    public void delete(String tableName, String _id) {

        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete(tableName, "id=?", new String[] { _id });
        closeDataBase(db);
    }

    /**
     * 按条件查询数据
     * 
     * @param tableName
     * @param columns
     * @param selection
     * @return
     */
    public Cursor query(String tableName, String[] columns, String selection) {

        SQLiteDatabase db = helper.getReadableDatabase();  
        return db.query(tableName, columns, selection, null, null, null, null);
    } 
    
    /**
     * 更新记录信息
     * @param tableName 表名称
     * @param contentValues 更新的值
     * @param id 要更新的记录ID
     * @return 影响的记录条数
     */
    public int update(String tableName, ContentValues contentValues, String whereClause,String[] whereArgs) {
        int value = -1;
        
        SQLiteDatabase db = helper.getReadableDatabase();
        value = db.update(tableName, contentValues, whereClause, whereArgs);
        
        closeDataBase(db);
        return value;
    }  

    /**
     * 关闭数据库连接
     * 
     * @param database
     */
    private void closeDataBase(SQLiteDatabase database) {
        // TODO Auto-generated method stub
        if (database != null && database.isOpen()) {
            database.close();
            database = null;
        }
    }

    /**
     * 关闭整个数据库
     */
    public void close() {

        if (helper != null) {
            helper.close();
            helper = null;
        }
        mInstance = null;
    }
}
